#include "OpenGLDriverWin32.h"
#ifdef _MSC_VER
#pragma comment(lib, "OpenGL32.lib")
#endif
#include <windows.h>
#include <gl/GL.h>

COpenGLDriverWin32::COpenGLDriverWin32(StartDriverParams *DrvParams):COpenGLDriver(DrvParams)
{
}

COpenGLDriverWin32::~COpenGLDriverWin32()
{
}

void COpenGLDriverWin32::BeginRender()
{
	COpenGLDriver::BeginRender();
}

void COpenGLDriverWin32::EndRender()
{
	COpenGLDriver::EndRender();
	SwapBuffers(hDC);
}

void COpenGLDriverWin32::EnableOpenGL(HWND hwnd,StartDriverParams *DrvParams)
{
	PIXELFORMATDESCRIPTOR pfd;

	int iFormat;

	hDC = GetDC(hwnd);

	ZeroMemory(&pfd, sizeof(pfd));

	pfd.nSize = sizeof(pfd);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;
	pfd.cDepthBits = 0;
	pfd.iLayerType = PFD_MAIN_PLANE;

	iFormat = ChoosePixelFormat(hDC, &pfd);

	SetPixelFormat(hDC,iFormat, &pfd);
	hRC = wglCreateContext(hDC);

	wglMakeCurrent(hDC,hRC);

#ifdef WGL_EXT_swap_control
	PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
	// vsync extension
	wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
	// set vsync
	if (wglSwapIntervalEXT)
		wglSwapIntervalEXT(DrvParams->VSync ? 1 : 0);
#endif

	InitDriver();
}
